/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entity.MUser;
import entity.SrcCureplanDoctor;
import entity.SrvCureplan;
import entity.SrvCureservice;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.MSpecialist;
import session.MSpecialistFacade;
import entity.MHotel;
import session.MHotelFacade;

/**
 *
 * @author nchanwut
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class SrcCureplanDoctorManager {

    @PersistenceContext(unitName = "MedicalHub-warPU")
    private EntityManager em;
    @Resource
    private SessionContext context;
    @EJB
    private SrvCureplanFacade srvCureplanFacade;
    @EJB
    private SrcCureplanDoctorFacade srcCureplanDoctorFacade;
    @EJB
    private MUserFacade mUserFacade;
    @EJB
    private SrvCureserviceFacade srvCureserviceFacade;
    @EJB
    private MSpecialistFacade mSpecialistFacade;
    @EJB
    private MHotelFacade mHotelFacade;

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public SrcCureplanDoctor SaveCurePlanDoctorDetail(String txtSymptomComment, String txtAllergicHistoryComment, String txtDiseaseHistoryComment, String txtAttachHistoryComment, String txtRoomTypeComment, String txtPriceMinAcceptComment, String txtPriceMaxAcceptComment, String datDateMinAcceptComment, String datDateMaxAcceptComment, String uplAttachQuotation, String ddlHotelComment, String ddlSpecialistComment, String curePlanDoctorId, String btnSubmit, String btnReply) {
        try {
            MUser mUser = mUserFacade.find((long) 1);
            if (mUser == null) {
                //@TODO : Throw Exception message error
            }
            //Check if has 'curePlanDoctorId'
            String curePlanId = "";
            if (!curePlanDoctorId.isEmpty()) {
                curePlanId = srcCureplanDoctorFacade.find(Long.parseLong(curePlanDoctorId)).getSrvCureplan().getId().toString();
            }
            SrvCureplan srvCureplan = srvCureplanFacade.find(Long.parseLong(curePlanId));
            if (srvCureplan == null) {
                //@TODO : Throw Exception message error
            }

            MSpecialist mSpecialist = null;
            MHotel mHotel = null;
            if(!ddlSpecialistComment.isEmpty())
            mSpecialist = mSpecialistFacade.find(Long.parseLong(ddlSpecialistComment));
            if(!ddlHotelComment.isEmpty())
            mHotel = mHotelFacade.find(Long.parseLong(ddlHotelComment));

            String strStatus = "W";
            if (!btnSubmit.isEmpty()) {
                strStatus = "F";
            } else if (!btnReply.isEmpty()) {
                strStatus = "E";
            } else {
                //@TODO : Throw Exception message error
            }

            SrcCureplanDoctor srcCureplanDoctor = saveSrvCureplanDoctor(txtSymptomComment, txtAllergicHistoryComment, txtDiseaseHistoryComment, txtAttachHistoryComment, txtRoomTypeComment, txtPriceMinAcceptComment, txtPriceMaxAcceptComment, datDateMinAcceptComment, datDateMaxAcceptComment, uplAttachQuotation, mHotel, mSpecialist, curePlanDoctorId, strStatus);
            updateSrvCureplan(srvCureplan, strStatus);

            em.flush();
            return srcCureplanDoctor;
        } catch (Exception e) {
            context.setRollbackOnly();
            return null;
        }
    }

    private SrcCureplanDoctor saveSrvCureplanDoctor(String txtSymptom, String txtAllergicHistory, String txtDiseaseHistory, String txtAttachHistory, String txtRoomType, String txtPriceMinAccept, String txtPriceMaxAccept, String datDateMinAccept, String datDateMaxAccept, String uplAttachQuotation, MHotel mHotel, MSpecialist mSpecialist, String curePlanDoctorId, String strStatus) throws ParseException {
        SrcCureplanDoctor srcCureplanDoctor;
        if (curePlanDoctorId.isEmpty()) { //New
            srcCureplanDoctor = new SrcCureplanDoctor();
        } else { //Update
            srcCureplanDoctor = srcCureplanDoctorFacade.find(Long.parseLong(curePlanDoctorId));
            //@TODO : ถ้าไม่มี object ให้ Throw Exception message error
        }
        DateFormat df = new SimpleDateFormat("MM/dd/yyyy", Locale.US);


        srcCureplanDoctor.setSymptom(txtSymptom);
        srcCureplanDoctor.setAllergicHistory(txtAllergicHistory);
        srcCureplanDoctor.setDiseaseHistory(txtDiseaseHistory);
        srcCureplanDoctor.setAttachHistoryComment(txtAttachHistory);
        srcCureplanDoctor.setRoomType(txtRoomType);
        if (!txtPriceMinAccept.isEmpty()) {
            srcCureplanDoctor.setPriceMinAccept(txtPriceMinAccept);
        } else {
            srcCureplanDoctor.setPriceMinAccept(null);
        }
        if (!txtPriceMaxAccept.isEmpty()) {
            srcCureplanDoctor.setPriceMaxAccept(txtPriceMaxAccept);
        } else {
            srcCureplanDoctor.setPriceMaxAccept(null);
        }
        if (!datDateMinAccept.isEmpty()) {
            srcCureplanDoctor.setDateMinAccept(datDateMinAccept);
        } else {
            srcCureplanDoctor.setDateMinAccept(null);
        }
        if (!datDateMaxAccept.isEmpty()) {
            srcCureplanDoctor.setDateMaxAccept(datDateMaxAccept);
        } else {
            srcCureplanDoctor.setDateMaxAccept(null);
        }
        srcCureplanDoctor.setAttachQuotation(uplAttachQuotation);
        srcCureplanDoctor.setMSpecialist(mSpecialist);
         srcCureplanDoctor.setMHotel(mHotel);

        em.persist(srcCureplanDoctor);
        return srcCureplanDoctor;
    }

    private void updateSrvCureplan(SrvCureplan srvCureplan, String strStatus) {

        // set up customer order
        SrcCureplanDoctor srcCureplanDoctor;
        if (srvCureplan != null) {
            srvCureplan.setStatus(strStatus);

            em.persist(srvCureplan);
        } else {
            //@TODO : Throw Exception message error
        }

    }
}
